#pragma once
#include<iostream>

#include<assert.h>
using namespace std;


namespace lh
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin()const
		{
			return _str;
		}
		const_iterator end()const
		{
			return _str + _size;
		}
		//构造函数，非自定义类型，不推崇初始化列表,全省默认传"\0"
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		//传统写法
		//string(const string& s)
		//	:_capacity(s._capacity)
		//	, _size(s._size)
		//	, _str(new char[_capacity + 1])

		//{
		//	strcpy(_str, s._str);
		//}

		//现代写法	找打工人
		void swap(string& tmp)
		{
			::swap(_str, tmp._str);
			::swap(_capacity, tmp._capacity);
			::swap(_size, tmp._size);
		}
		string(const string& s)
			:_str(nullptr)
			, _capacity(0)
			, _size(0)
		{
			string tmp(s._str);
			//swap(_str, tmp._str);
			//swap(_capacity, tmp._capacity);
			//swap(_size, tmp._size);
			swap(tmp);
		}
		////传统写法
		//string& operator=(const string& s)
		//{
		//	if (this != &s)
		//	{
		//		char* tmp = new char[s._capacity + 1];
		//		strcpy(tmp, s._str);
		//		delete[] _str;

		//		_str = tmp;
		//		_size = s._size;
		//		_capacity = s._capacity;
		//	}
		//	return *this;
		//}
		
		//现代写法：找打工人
		//string& operator=(const string& s)
		//{
		//	if (this != &s)
		//	{
		//		//string tmp(s._str);
		//		string tmp(s);
		//		swap(tmp);
		//	}
		//}

		string& operator=( string s)
		{
			swap(s);
			return *this;
		}


		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}
		const char* c_str()const
		{
			return _str;
		}

		size_t size()const
		{
			return _size;
		}
		size_t capacity()const
		{
			return _capacity;
		}

		bool empty()const
		{
			return _size == 0;
		}

		const char& operator[](size_t pos)const
		{
			assert(pos < _size);
			return _str[pos];
		}
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		//开空间
		void reserve(size_t n)
		{
			//如果需要扩容
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);				//复制内容给临时变量,释放原空间内容，指向新空间，更新容量
				delete[] _str;

				_str = tmp;
				_capacity = n;
			}
		}

		//开空间＋初始化
		void resize(size_t n, char ch = '\0')
		{
			if (n > _size)
			{
				//插入数据
				reserve(n);

				for (size_t i = _size; i < n; i++)
				{
					_str[i] = ch;
				}
				_str[n] = '\0';
				_size = n;

			}
			else
			{
				//删除数据
				_str[n] = '\0';
				_size = n;
			}
		}

		void push_back(char ch)
		{
			////满了扩容
			//if (_size == _capacity)
			//{
			//	reserve(_capacity == 0 ? 4 : _capacity * 2);
			//}
			//_str[_size] = ch;
			//++_size;
			//_str[_size] = '\0';

			//复用
			insert(_size, ch);
		}

		void append(const char* str)
		{
			//size_t len = strlen(str);
			////满了扩容
			//if (_size+len> _capacity)
			//{
			//	reserve(_size+len);
			//}
			//strcpy(_str + _size, str);	//'\0'也被拷贝.
			//_size += len;


			//复用
			insert(_size, str);
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		string& insert(size_t pos,char ch)
		{
			assert(pos <= _size);
			//满了扩容
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			//末尾后一个
			size_t end = _size + 1;
			while (end > pos)
			{
				//前一个数据后移
				_str[end] = _str[end - 1];
				--end;
			}
			_str[pos] = ch;
			_size++;
			return *this;
		}

		string& insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			//满了扩容
			if (_size +len> _capacity)
			{
				reserve(_size+len);
			}
			//末尾后len个位置
			size_t end = _size + len;
			while (end >= pos+len)
			{
				//数据后移
				_str[end] = _str[end - len];
				--end;
			}
			strncpy(_str+pos,str,len);
			_size += len;
			return *this;
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);
			if (len == npos || pos + len >= _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
		}
		
		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		size_t find(char ch, size_t pos = 0) const
		{
			assert(pos < _size);
			for (size_t i = pos; i < _size; i++)
			{
				if (ch == _str[i])
					return i;
			}
			return npos;
		}

		size_t find(const char* sub, size_t pos = 0) const
		{
			assert(sub);
			assert(pos < _size);
			const char* ptr = strstr(_str + pos, sub);
			if (ptr == nullptr)
			{
				return npos;
			}
			else
			{
				return ptr - _str;
			}

		}
		//从pos开始截取一段指定长度字符串
		string substr(size_t pos, size_t len = npos) const
		{
			assert(pos < _size);
			//截取字符串的真实长度
			size_t realLen = len;
			if (len == npos || pos + len > npos)
			{
				realLen = _size - pos;
			}


			string sub;
			for (size_t i = 0; i < realLen; i++)
			{
				sub += _str[pos + i];
			}
			return sub;
		}

		bool operator>(const string& s) const
		{
			return strcmp(_str, s._str) > 0;
		}

		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}

		bool operator>=(const string& s) const
		{
			return (*this > s) || (*this == s);
		}

		bool operator<=(const string& s) const
		{
			return !(*this > s);
		}

		bool operator<(const string& s) const
		{
			return !(*this >= s);
		}

		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}



	private:
		size_t _size;
		size_t _capacity;
		char* _str;
	public:
		//C++特例，const static特性，直接定义初始化
		const static size_t npos = -1;
	};

	//流提取
	ostream& operator<<(ostream& out, const string& s) 
	{
		for (int i = 0; i < s.size(); i++)
		{
			out << s[i];
		}
		return out;
	}
	//流插入
	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch;
		ch = in.get();
		//类似缓冲区思路
		const size_t N = 32;
		char buff[N];
		size_t i= 0;
		while (ch != ' ' && ch != '\0')
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		buff[i] = '\0';
		s += buff;
		return in;
	}


	void test_string1()
	{
		//std::string s1("hello world");
		//std::string s2;
		string s1("hello world");
		string s2;

		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

		for (size_t i = 0; i < s1.size(); ++i)
		{
			cout << s1[i] << " ";
		}
		cout << endl;

		for (size_t i = 0; i < s1.size(); ++i)
		{
			s1[i]++;
		}

		for (size_t i = 0; i < s1.size(); ++i)
		{
			cout << s1[i] << " ";
		}
		cout << endl;
	}
	void test_string2()
	{
		string s1("hello");
		cout << s1.c_str() << endl;
		s1 += ' ';
		s1.append("world");
		cout << s1.c_str() << endl;
		s1.insert(5,'#');
		cout << s1.c_str() << endl;
		s1.insert(0, '#');
		cout << s1.c_str() << endl;

		s1.insert(1, "!!!");
		cout << s1.c_str() << endl;

	}
	void test_string3()
	{
		string s1("hello");
		cout << s1.c_str() << endl;

		s1.erase(1, 3);
		cout << s1.c_str() << endl;

	}

	void test_string4()
	{
		string s1("hello world");
		cout<<s1.find('l',5)<<endl;

		cout << s1.find("world", 1) << endl;
	}
}
